/*     */ package cn.com.hongpay.client.utils;
/*     */ 
/*     */ import java.io.FileInputStream;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.security.KeyStore;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.PublicKey;
/*     */ import java.security.Signature;
/*     */ import java.security.cert.Certificate;
/*     */ import java.security.cert.CertificateFactory;
/*     */ import java.util.Enumeration;
/*     */ 
/*     */ public class SignatureTools
/*     */ {
/*     */   private static final String PKCS12 = "PKCS12";
/*     */   private static final String X509 = "X.509";
/*     */   private static final String SHA1withRSA = "SHA1withRSA";
/*     */ 
/*     */   private static PrivateKey loadPrivateKey(String pfxFile, String pfxPwd, String privateKeyPwd)
/*     */   {
/*     */     try
/*     */     {
/*  20 */       InputStream bis = new FileInputStream(pfxFile);
/*  21 */       KeyStore store = KeyStore.getInstance("PKCS12");
/*  22 */       store.load(bis, pfxPwd.toCharArray());
/*  23 */       String alias = (String)store.aliases().nextElement();
/*  24 */       return (PrivateKey)store.getKey(alias, privateKeyPwd.toCharArray());
/*     */     } catch (Exception e) {
/*  26 */       System.out.println("load private key failed.");
/*  27 */       e.printStackTrace();
/*  28 */     }return null;
/*     */   }
/*     */ 
/*     */   private static PublicKey loadPublicKey(String cerFile)
/*     */   {
/*     */     try {
/*  34 */       InputStream bis = new FileInputStream(cerFile);
/*  35 */       CertificateFactory cf = CertificateFactory.getInstance("X.509");
/*  36 */       Certificate cert = cf.generateCertificate(bis);
/*  37 */       return cert.getPublicKey();
/*     */     } catch (Exception e) {
/*  39 */       System.out.println("load public key failed.");
/*  40 */       e.printStackTrace();
/*  41 */     }return null;
/*     */   }
/*     */ 
/*     */   public static String sign(String plainText, String charset, String pfxFile, String pfxPwd, String privateKeyPwd)
/*     */   {
/*     */     try
/*     */     {
/*  56 */       Signature signature = Signature.getInstance("SHA1withRSA");
/*  57 */       signature.initSign(loadPrivateKey(pfxFile, pfxPwd, privateKeyPwd));
/*  58 */       if ((charset != null) && (charset.length() > 0))
/*  59 */         signature.update(plainText.getBytes(charset));
/*     */       else {
/*  61 */         signature.update(plainText.getBytes());
/*     */       }
/*  63 */       byte[] sign = signature.sign();
/*  64 */       return new String(CommonTools.hex2Ascii(sign));
/*     */     } catch (Exception e) {
/*  66 */       e.printStackTrace();
/*     */     }
/*  68 */     return null;
/*     */   }
/*     */ 
/*     */   public static String sign(String plainText, String pfxFile, String pfxPwd, String privateKeyPwd)
/*     */   {
/*  80 */     return sign(plainText, null, pfxFile, pfxPwd, privateKeyPwd);
/*     */   }
/*     */ 
/*     */   public static boolean verifySignature(String sign, String plainText, String cerFile)
/*     */   {
/*     */     try
/*     */     {
/*  92 */       PublicKey key = loadPublicKey(cerFile);
/*  93 */       byte[] signArray = CommonTools.ascii2Hex(sign.getBytes());
/*  94 */       Signature signature = Signature.getInstance("SHA1withRSA");
/*  95 */       signature.initVerify(key);
/*  96 */       signature.update(plainText.getBytes());
/*  97 */       return signature.verify(signArray);
/*     */     } catch (Exception e) {
/*  99 */       e.printStackTrace();
/*     */     }
/* 101 */     return false;
/*     */   }
/*     */ 
/*     */   public static boolean verifySignatureWithCharset(String sign, String plainText, String cerFile, String charset)
/*     */   {
/*     */     try
/*     */     {
/* 114 */       PublicKey key = loadPublicKey(cerFile);
/* 115 */       byte[] signArray = CommonTools.ascii2Hex(sign.getBytes());
/* 116 */       Signature signature = Signature.getInstance("SHA1withRSA");
/* 117 */       signature.initVerify(key);
/* 118 */       signature.update(plainText.getBytes(charset));
/* 119 */       return signature.verify(signArray);
/*     */     } catch (Exception e) {
/* 121 */       e.printStackTrace();
/*     */     }
/* 123 */     return false;
/*     */   }
/*     */ }

/* Location:           F:\hongpay_client.jar
 * Qualified Name:     cn.com.hongpay.client.utils.SignatureTools
 * JD-Core Version:    0.6.0
 */